#!/usr/bin/env python
# -*- coding: utf-8 -*-
#=======================================#
# @author: RedSoxFan and NerfSoldier
# @copyright: 2012 RedSoxFan and NerfSoldier
# @license: GNU General Public License v3.0 
#           <http://www.gnu.org/copyleft/gpl.html>
# @summary: Handles Logging
#=======================================#

import sys
import time
 
class Logger(object):
    LEVEL_SUPRESS = (0,"SUPRESS") # Do not log anything unless it is raw
    LEVEL_FATAL   = (1,"FATAL")   # Only log FATAL errors
    LEVEL_ERROR   = (2,"ERROR")   # Log FATAL and ERROR Levels
    LEVEL_WARNING = (3,"WARNING") # Log FATAL, ERROR, and WARNING Levels
    LEVEL_INFO    = (4,"INFO")    # Log FATAL, ERROR, WARNING, INFO Levels
    LEVEL_DEBUG   = (5,"DEBUG")   # Log Everything
    def __init__(self,allow=LEVEL_ERROR):
        '''
        Logger
        keywords:
            allow - Log Allow Level (default is ERROR)
        '''
        self.allow = allow
    def log(self,level,msg):
        '''
        Log message if level is lower than or equal to allow threshold
        '''
        if level[0]<=self.allow[0]:
            sys.stderr.write("(%s) %s: %s\r\n"%(time.strftime("%d %b %y %H:%M:%S"),level[1].upper(),msg))
            sys.stderr.flush()
    def debug(self,msg):
        '''
        Log message with level DEBUG
        '''
        self.log(Logger.LEVEL_DEBUG, msg)
    def info(self,msg):
        '''
        Log message with level INFO
        '''
        self.log(Logger.LEVEL_INFO, msg)
    def warning(self,msg):
        '''
        Log message with level WARNING
        '''
        self.log(Logger.LEVEL_WARNING, msg)
    def error(self,msg):
        '''
        Log message with level ERROR
        '''
        self.log(Logger.LEVEL_ERROR, msg)
    def fatal(self,msg):
        '''
        Log message with level FATAL
        '''
        self.log(Logger.LEVEL_FATAL, msg)
    @staticmethod
    def raw(msg,tim=True):
        '''
        Log Raw Message
        if tim is True (default), the current time will 
        be prepended to the message
        '''
        if tim: sys.stderr.write("(%s) %s\r\n"%(time.strftime("%d %b %y %H:%M:%S"),msg))
        else: sys.stderr.write("%s\r\n"%msg)
        sys.stderr.flush()